package com.xsdl.template;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserServiceImpl extends AbstractTemplateServiceImpl<User> {

    private List<User> userList = new ArrayList<>();

    public void addUser(User user) {
        saveToDataBase(user);
    }

    @Override
    protected User getEntity(String uuid) {
        return userList.stream().filter(item -> item.getUuid().equals(uuid)).findFirst().orElse(null);
    }

    @Override
    protected User saveToDataBase(User user) {
        User exist = getEntity(user.getUuid());
        if (exist == null) {
            userList.add(user);
            return user;
        }
        userList.remove(exist);
        userList.add(user);
        return user;
    }

    @Override
    protected String getResourceName() {
        return "用户";
    }

    public List<User> getUserList() {
        return Collections.unmodifiableList(userList);
    }

}
